home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / clantutr.zip / TEST1-5 < prev    next >
Text File  |  1985-12-30  |  14KB  |  436 lines

  1. .T
  2.    and now a TEST   
  3. .R4C1
  4. 1 ~b~Imain() {                            ~N
  5. 2 ~b~I    char key;                       ~N
  6. 3 ~b~I    printf("\n Press a key ");      ~N
  7. 4 ~b~I    scanf("%c",key);                ~N
  8. 5 ~b~I    printf("\n You pressed %c",key);~N
  9. 6 ~b~I}                                   ~N
  10.  
  11. .Q
  12. Which line has the error?
  13. 4
  14.  
  15.     Line ~I4~N should read:
  16.  
  17. 4 ~b~I    scanf("%c",~F&~N~b~Ikey);               ~N
  18. .K19,60
  19. Iknowthat!
  20. .WN
  21.  
  22.  
  23. 1 ~b~Imain() {                            ~N
  24. 2 ~b~I    char key;                       ~N
  25. 3 ~b~I    printf("\n Press a key ");      ~N
  26. 4 ~b~I    scanf("%c",&key);               ~N
  27. 5 ~b~I    printf("\n You pressed %c",key) ~N
  28. 6 ~b~I}                                   ~N
  29.  
  30. .Q
  31. Which line has the error?
  32. 5
  33.  
  34.     Line ~I5~N should read:
  35.  
  36. 5 ~b~I    printf("\n You pressed %c",key)~F;~N
  37. .K19,60
  38. ;;;;;;;;;;
  39. .WN
  40.  
  41. .Q
  42. If x is an integer variable, then where in memory is it located?
  43. &x
  44.  
  45. The ~Iaddress~N, in memory, of any variable ~b~Ix~N is ~b~I&x~N
  46. .K16,30
  47. &x=address
  48. .R9C1
  49. .Q
  50. &x is called a ?
  51. pointer
  52. .K16,30
  53. pointertox
  54. .WN
  55. 1 ~b~Imain() {                       ~N
  56. 2 ~b~I    int i;                     ~N
  57. 3 ~b~I    char x='A';                ~N
  58. 4 ~b~I    for (i=0; i<26; i++,x++)   ~N
  59. 5 ~b~I        printf("%c",x);        ~N
  60. 6 ~b~I}                              ~N
  61. .Q
  62. How many errors can you find ?
  63. 0
  64. .R8C60
  65. ~INO ERRORS!~N
  66. .R11C1
  67. 4 ~b~I    for (i=0; i<26; i++,~Fx++~N~b~I)   ~N
  68.  
  69.     Although ~b~Ix~N is declared a ~b~Ichar~N and given the 'value' ~b~IA~N,
  70.     the actual number stored in memory is (probably!) ~I65~N (in decimal)
  71.     which is the ASCII value for an ~IA~N. This ~I65~N is incremented via ~b~Ix++~N
  72.     and ~b~Iprintf()~N will print: ~r~IABCDEFGHIJKLMNOPQRSTUVWXYZ~N
  73. .K19,60
  74. probably??
  75. .W
  76. .R7C1B
  77. ~V          DON'T TRUST THE INTERNAL COMPUTER 'VALUE' TO BE ASCII!!         
  78. .W
  79. .R7C1B
  80. ~V   however, if x is the 'A'-value, x+1 SHOULD be the 'B'-value...right?   
  81. .WK8,30
  82.   RIGHT!
  83. .WN
  84. 1 ~b~Imain() {                       ~N
  85. 2 ~b~I    printf("%s,%s,%s",         ~N
  86. 3 ~b~I        "Now is the time",     ~N
  87. 4 ~b~I        "for all good me",     ~N
  88. 5 ~b~I        "n to come to th",     ~N
  89. 6 ~b~I        "e aid of thier ",     ~N
  90. 7 ~b~I        "country.";            ~N
  91. 8 ~b~I}                              ~N
  92. .Q
  93. How many errors here?
  94. 2
  95. ~V  i-before-e-except-after-c ? ~N
  96. .R6C1
  97. 6 ~b~I        "he aid of ~Fthier~N~b~I",     ~N
  98. .K2,50
  99.   their
  100. .WR14C1
  101.     ...and the printout would be:
  102.  
  103. ~r~INow is the timefor all good men to come to the aid of thier country.~N
  104.               
  105.             tch!tch!
  106. .WN
  107. 1 ~b~Imain() {                         ~N
  108. 2 ~b~I    int w;                       ~N
  109. 3 ~b~I    char z='z';                  ~N
  110. 4 ~b~I    w=z;                         ~N
  111. 5 ~b~I    while (z>'w');               ~N
  112. 6 ~b~I        printf("%c=%d;",z--,w--);~N
  113. 7 ~b~I}                                ~N
  114.  
  115. .Q
  116. Which line has the error ?
  117. 5
  118.     Line 5 shouldn't have a semi-colon! (NOT after ~Iwhile~N or ~Ifor~N):
  119. .R5C1
  120. 5 ~b~I    while (z>'w')~F;~N~b~I               ~N
  121. .WK2,50
  122. z--,w--???
  123. .R14C1
  124.     Line 4 sets ~b~Iw~N to the ~Ivalue~N of ~b~Iz~N ( 122 in ASCII ).
  125.     Line 6 is executed, ~b~Iprintf~N-ing the ~b~I%c~Nhar ~b~Iz~N and the ~b~I%d~Necimal
  126.     ~b~Iw~N, then both ~b~Iz~N and ~b~Iw~N are ~IDECREMENTED~N (via ~b~Iz--,w--~N),
  127.     and line 6 is repeated ...as long as ~b~I%c~Nharacter ~b~Iz~N is greater than
  128.     ~b~I'w'~N (meaning the 'value' of ~b~Iz~N exceeds the 'value' of the character
  129.     ~b~Iw~N, which, in ASCII, is 119).
  130.  
  131. .W
  132.     The printout would be:
  133.  
  134. ~r~Iz=122;y=121;x=120;~N
  135. .WK2,50
  136. confusing!
  137. .WN
  138. 1 ~b~Imain() {                         ~N
  139. 2 ~b~I    char s;                      ~N
  140. 3 ~b~I    s="I'm a string";            ~N
  141. 4 ~b~I        printf("%s",&s);         ~N
  142. 5 ~b~I}                                ~N
  143. .Q
  144. Which line has the error(s) ?
  145. 4
  146.     Line 4 should be:
  147.  
  148. 4 ~b~I        printf("%s",s);          ~N    ...without the ~b~I&~N !
  149. .R13C1
  150.     Although ~b~Iprintf()~N expects to recieve the ~Iaddress~N of a ~b~I%s~Ntring
  151.     variable (such as ~b~Is~N), we must ~V let C do it~N!  We just refer to ~b~Is~N,
  152.     in ~b~Iprintf("%s",s)~N, and the C-compiler will look after passing the address
  153.     to ~b~Iprintf()~N.
  154. .W
  155. .R13C1
  156.     Although ~b~Iprintf()~N expects to ~Frecieve~N the ~Iaddress~N of a ~b~I%s~Ntring
  157. .K2,50
  158.   sorry!
  159. .WR13C1
  160.     Although ~b~Iprintf()~N expects to receive the ~Iaddress~N of a ~b~I%s~Ntring
  161. .R18C1
  162.     The printout might be:
  163.  
  164. ~r~I∞I~N      ...or some other garbage!
  165. .WNT
  166.  single CHARacters and INTegers are the same ? 
  167. .R4C1
  168. 1 ~b~Imain() {                         ~N
  169. 2 ~b~I    char x;                      ~N
  170. 3 ~b~I    for (x=0; x<=255; x++)       ~N
  171. 4 ~b~I        printf("%c",x);          ~N
  172. 5 ~b~I}                                ~N
  173. .Q
  174. Will this print the IBM PC character set ..once ?
  175. n
  176.     When we increment ~b~Ix~N from ~b~I0~N to ~b~I255~N we ~IDO~N get a
  177.     printout of the ~b~I%c~Nharacters whose 'values' are ~I0~N, ~I1~N, ~I2~N, etc.
  178.     BUT, because a ~b~I%c~Nharacter ~IALWAYS~N occupies a single byte in memory
  179.     their 'values' are ~IALWAYS~N ~b~I<=255~N (less-than-or-equal-to 255).
  180.     SO ...incrementing ~b~Ix~N, when it has the 'value' ~I255~N, will give the 'value'
  181.     ~b~I0~N ...and the above program will continue to cycle through the character
  182.     set ...forever!
  183. .K4,65
  184.  forever?
  185. .W
  186. 1 ~b~Imain() {                         ~N  But ~Ithis~N program WILL stop
  187. 2 ~b~I    int x;                       ~N  when the ~b~I%int~Neger ~b~Ix~N
  188. 3 ~b~I    for (x=0; x<=255; x++)       ~N  reaches the value ~I256~N, since
  189. 4 ~b~I        printf("%c",x);          ~N  ~b~Iint~Negers CAN be larger than ~I255~N
  190. 5 ~b~I}                                ~N
  191. .K4,65
  192. int is big
  193. .WN
  194. 1 ~b~Imain() {                         ~N
  195. 2 ~b~I    float x, y;                  ~N
  196. 3 ~b~I    printf("Enter two numbers);  ~N
  197. 4 ~b~I    scanf("%f%f",x,y);           ~N
  198. 5 ~b~I    printf("%s%f",               ~N
  199. 6 ~b~I        "Their sum is ",         ~N
  200. 7 ~b~I        sum(x,y);                ~N
  201. 8 ~b~I}                                ~N
  202. 9 ~b~Isum(a,b)                         ~N
  203. 10~b~I{                                ~N
  204. 11~b~Ifloat a, b;                      ~N
  205. 12~b~Ireturn(a+b);                     ~N
  206. 13~b~I}                                ~N
  207. .Q
  208. How many BUGS!@#$ ???
  209. 4
  210. .WR18C1
  211.     Forgot something in line 3....
  212. .R3C38
  213. ~b~I    printf("Enter two numbers~F"~N~b~I);  ~N
  214. .WR18C1
  215.     Forgot something in line 7 too!
  216. .R7C38
  217. ~b~I        sum(x,y)~F)~N~b~I;                ~N
  218. .WR18C1
  219.     Forgot to declare the function type (else we'd get ~Iintegers~N returned!)
  220. .R9C38
  221. ~b~I~Ffloat~N~b~I sum(a,b)                   ~N
  222. .WR18C1
  223.                                                                                
  224. .R18C1
  225.     We ~IMUST~N declare function arguments ~IIMMEDIATELY~N (~Ibefore~N the ~b~I{~N)!
  226. .R10C38
  227. ~b~Ifloat a, b;                      ~N wrong
  228. .R11C38
  229. ~b~I{                                ~N order
  230. .WN
  231. 1 ~b~Imain() {                                     ~N
  232. 2 ~b~I    int words=0;                             ~N
  233. 3 ~b~I    char c;                                  ~N
  234. 4 ~b~I    while ( (c=getchar())!='\r' && c!='\n') }~N
  235. 5 ~b~I        if (c == ' ')                        ~N
  236. 6 ~b~I            words++;                         ~N
  237. 7 ~b~I    }                                        ~N
  238. 8 ~b~I   printf("\n Number = %d",words);           ~N
  239. 9 ~b~I}                                            ~N
  240. .Q
  241. Any errors here (y/n) ?
  242. n
  243. .R10C60
  244. ~INO ERRORS~N
  245. .R4C1
  246. 4 ~b~I    while ( (~Vc=getchar()~N~b~I)!='\r' && c!='\n') }~N
  247. .R13C1
  248.     Here we invoke the C-library function ~b~Igetchar()~N which inputs a
  249.     ~Isingle~N character, which we assign to our ~b~Ichar~Nacter variable ~b~Ic~N.
  250. .WR4C1
  251. 4 ~b~I    while ( (c=getchar())~V!='\r' && c!='\n'~N~b~I) }~N
  252. .R13C1
  253.                                                                               
  254.                                                                               
  255.                                                                               
  256.                                                                               
  257.                                                                               
  258. .R13C1
  259.     Here we check to see that the ~b~Ic~Nharacter is NOT EQUAL (~b~I!=~N) to a
  260.     ~b~I\r~Neturn AND (~b~I&&~N) NOT EQUAL to a ~b~I\n~Newline. ( This then
  261.     will exit the ~b~Iwhile~N loop when we press the ~IEnter~N key ).
  262.     As long as it's NOT EQUAL, we execute the ~b~Iwhile~N loop.           
  263. .K2,60
  264. &&=AND !!
  265. .WR4C1
  266. 4 ~b~I    while ( (c=getchar())!='\r' && c!='\n') }~N
  267. 5 ~V        if (c == ' ')                ~N
  268. 6 ~V            words++;                 ~N
  269. .R13C1
  270.                                                                               
  271.                                                                               
  272.                                                                               
  273.                                                                               
  274.                                                                               
  275. .R13C1
  276.     Inside the ~b~Iwhile~N, we check if the ~b~Ic~Nharacter ~b~I== ' '~N          
  277.     (~IEQUAL~N a space) and, if it is, we increment the ~b~Iint~Neger ~b~Iwords~N.     
  278. .WR5C1
  279. 5 ~b~I        if (c == ' ')                ~N
  280. 6 ~b~I            words++;                 ~N
  281. 7 ~V    }                                ~N
  282. .R13C1
  283.                                                                               
  284.                                                                               
  285.                                                                               
  286.                                                                               
  287.                                                                               
  288. .R13C1
  289.     The first time that the ~IEnter~N key ~b~Ic~Nharacter occurs, we exit the
  290.     ~b~Iwhile~N loop ...and this ~b~I}~N defines the ~Iend-of-the-while~N.
  291. .WR7C1
  292. 7 ~b~I    }                                ~N
  293. 8 ~V   printf("\n Number = %d",words);   ~N
  294. .R13C1
  295.                                                                               
  296.                                                                               
  297.                                                                               
  298.                                                                               
  299.                                                                               
  300. .R13C1
  301.     After leaving the ~b~Iwhile~N, we ~b~Iprintf()~N the value of ~b~Iwords~N, which
  302.     may (or may not!) agree with the number of 'words' typed.                 
  303.     (It WILL if you ~Ifollow~N every word with ~Ione~N space!)
  304. .WR8C1
  305. 8 ~b~I   printf("\n Number = %d",words);   ~N
  306. 9 ~V}                                    ~N
  307. .R13C1
  308.                                                                               
  309.                                                                               
  310.                                                                               
  311.                                                                               
  312.                                                                               
  313. .R13C1
  314.     ...and the end of ~b~Imain()~N ( = end of the program! )
  315. .WR15C1
  316.     If we compiled/linked/ran this program, we'd get:
  317.  
  318. ~VThis is a line which has 7 words ~N       You type this, and press ~IEnter~N.
  319. ~r~I Number = 8~N                           The computer responds thus!
  320. .WK2,60
  321.   SMART!
  322. .WN
  323.     Suppose we have 3 ~b~Ichar~Nacter variables: ~b~Ix='A' ; y='a' ; z='B'~N
  324. .Q
  325. Is x!=y  (y/n)? 
  326. y
  327. .Q
  328. Is x!=z  (y/n)? 
  329. y
  330. .Q
  331. Is x!=x  (y/n)? 
  332. n
  333. .Q
  334. Is x!=y && x!=z (y/n)? 
  335. y
  336. .WR15C1
  337.     Since x ~IIS~N different from y,    the first  answer is ~Iy~N.
  338.     Since x ~IIS~N different from z,    the second answer is ~Iy~N.
  339.     Since x ~IIS NOT~N different from x, the third answer is ~In~N.
  340.     Since x ~IIS~N different from y ~IAND~N x ~IIS~N different from z
  341.                                     the fourth answer is ~Iy~N.
  342. .WN
  343. 0 ~b~I#include <stdio.h>  ~N
  344. 1 ~b~Imain()  {           ~N
  345. 2 ~b~Ifloat x=34.56;      ~N
  346. 3 ~b~Iprintf("%6.1f",x);  ~N
  347. 4 ~b~Iprintf("%-6.1f",x); ~N
  348. 5 ~b~Iprintf("%06.1f",x); ~N
  349. 6 ~b~I}                   ~N
  350.  
  351. .Q
  352. What will be printed in Line 3 ? 
  353.   34.6
  354. .Q
  355. What will be printed in Line 4 ? 
  356. 34.6  
  357. .Q
  358. What will be printed in Line 5 ? 
  359. 0034.6
  360. .WN
  361. .Q
  362. A C-expression meaning: n=n+1 
  363. n++
  364. .Q
  365. Another C-expression meaning: n=n+1 
  366. ++n
  367. .Q
  368. A C-expression meaning: n=n+5 
  369. n+=5
  370. .Q
  371. A C-expression meaning: n=n/7
  372. n/=7
  373. .Q
  374. A C-expression meaning: n=n-2 
  375. n-=2
  376. .Q
  377. A C-expression meaning: n=n*9
  378. n*=9
  379. .WN
  380. ~b~I    if ( (x IS GREATER THAN 5) AND (x IS NOT EQUAL TO 7) ) ~N
  381. ~b~I        if ( x IS EQUAL TO 9 )                             ~N
  382. .Q
  383. What C operator should replace IS GREATER THAN ?
  384. >
  385. .Q
  386. What C operator should replace AND ? 
  387. &&
  388. .Q
  389. What C operator should replace IS NOT EQUAL TO
  390. !=
  391. .Q
  392. What C operator should replace IS EQUAL TO
  393. ==
  394. .WN
  395. 1 ~b~Imain()  {           ~N
  396. 2 ~b~Iint i;              ~N
  397. 3 ~b~Ifloat x;            ~N
  398. 4 ~b~Ii=5.2;              ~N
  399. 5 ~b~Ix=i/2;              ~N
  400. 6 ~b~Iprintf("%f",x);     ~N
  401. 7 ~b~I}                   ~N
  402. .Q
  403. What will be printed ?
  404. 2.000000
  405.     In line 4, ~b~I5.2~N (a ~b~Ifloat~N) is converted to an ~b~Iint~N 'cause ~b~Ii~N
  406.     is declared to be an ~b~Iint~N in Line 2, so the number ~I5~N is assigned to ~b~Ii~N.
  407.  
  408.     In Line 5, because both ~b~Ii~N and ~b~I2~N are ~b~Iint~Negers ( no decinal in ~b~I2~N,
  409.     remember?), the division gives ~I5/2~N without the decimal part, ~I2~N, which
  410.     is then converted to ~b~Ifloat~N ('cause ~b~Ix~N is a ~b~Ifloat~N) and assigned
  411.     to ~b~Ix~N ....and ~b~Iprintf()~N gives 6 decimal places (unless told otherwise!).
  412. .WN
  413.  
  414.  
  415. .T
  416.    That's all folks!   
  417.  
  418.  
  419. .K16,32
  420. au revoir!
  421.  
  422. .q
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.